<?php

namespace app\admin\model\xilumarket\appeal;

use app\admin\model\xilumarket\order\Order;
use app\admin\model\xilumarket\user\User;
use app\common\model\xilumarket\AftersaleLog;
use think\Model;
use traits\model\SoftDelete;

class OrderAppeal extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xilumarket_order_appeal';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state_text',
        'images_text'
    ];
    

    
    public function getStateList()
    {
        return ['1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3')];
    }

    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getImagesTextAttr($value,$data){
        $images = [];
        $value = isset($data['images'])?$data['images']:'';
        if($value && is_string($value)){
            $images_array = explode(',',$value);
            foreach ($images_array as $image){
                $images[] = cdnurl($image,true);
            }
        }
        return $images;
    }

    public function user(){
        return $this->belongsTo(User::class,'user_id','id',[],'left')->setEagerlyType(0);
    }

    public function orders(){
        return $this->belongsTo(Order::class,'order_id','id',[],'left')->setEagerlyType(0);
    }

    public function aftersalelog(){
        return $this->hasMany(AftersaleLog::class,'order_id','order_id')->order('createtime','desc');
    }
}
